package com.nightpiter.view.dialogs;

import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.nightpiter.model.objects.Station;

/**
 * StationIcon class.
 * 
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public class StationIcon extends View {

	/**
	 * paints to draw the icon
	 */
	private Paint stationPaint, backPaint, lightPaint, linePaint;

	/**
	 * size of the point
	 */
	private static final int STATION_WIDTH = 20;

	private static final int STATION_BACK_WIDTH = STATION_WIDTH + 3;

	private static final int LIGHT_WIDTH = 5;

	private static final int BACK_LIGHT_WIDTH = 10;

	private static final int LINE_WIDTH = 10;

	private static final int LINE_BACK_WIDTH = LINE_WIDTH + 3;

	private static final int LINE_LIGHT_WIDTH = 4;

	private static final int PADDING = 10;

	private Hashtable<Integer, Integer> colorMap = new Hashtable<Integer, Integer>();

	/**
	 * @param context
	 */
	public StationIcon(Context context) {
		super(context);
	}

	/**
	 * @param context
	 * @param attrs
	 * @param inflateParams
	 */
	@SuppressWarnings("unchecked")
	public StationIcon(Context context, AttributeSet attrs, Map inflateParams) {
		super(context, attrs, inflateParams);
	}

	@Override
	protected void dispatchDraw(Canvas canvas) {
		if (stationPaint != null) {
			int x = getWidth() / 2;
			int y = getHeight() / 2;

			backPaint.setStrokeWidth(LINE_BACK_WIDTH);
			for (Entry<Integer, Integer> entry : colorMap.entrySet()) {
				int lineY = y + entry.getKey();
				canvas.drawLine(PADDING, lineY, getWidth() - PADDING, lineY,
						backPaint);
			}

			lightPaint.setStrokeWidth(LINE_LIGHT_WIDTH);
			for (Entry<Integer, Integer> entry : colorMap.entrySet()) {
				int lineY = y + entry.getKey();
				linePaint.setColor(entry.getValue());
				canvas.drawLine(PADDING, lineY, getWidth() - PADDING, lineY, linePaint);
				canvas.drawLine(PADDING, lineY + 1, getWidth() - PADDING, lineY + 1,
						lightPaint);
			}

			backPaint.setStrokeWidth(STATION_BACK_WIDTH);
			canvas.drawPoint(x, y, backPaint);
			canvas.drawPoint(x, y, stationPaint);

			lightPaint.setStrokeWidth(BACK_LIGHT_WIDTH);
			canvas.drawPoint(x + 1, y + 1, lightPaint);

			lightPaint.setStrokeWidth(LIGHT_WIDTH);
			canvas.drawPoint(x + 2, y + 2, lightPaint);
		}
		super.dispatchDraw(canvas);
	}

	/**
	 * @param station
	 *            the station to set
	 */
	public void setStation(Station station) {
		stationPaint = new Paint();
		stationPaint.setAntiAlias(true);
		stationPaint.setStrokeCap(Paint.Cap.ROUND);
		int linesCount = station.getLines().size();
		colorMap.clear();
		if (linesCount == 1) {
			int color = station.getLines().get(0).getColor();
			stationPaint.setColor(color);
			colorMap.put(0, color);
		} else {
			int startOffset = -(linesCount - 1) * LINE_WIDTH / 2;
			for (int i = 0; i < linesCount; i++) {
				colorMap.put(startOffset + i * LINE_WIDTH, station.getLines()
						.get(i).getColor());
			}
			stationPaint.setColor(Color.LTGRAY);
		}
		stationPaint.setStrokeWidth(STATION_WIDTH);

		backPaint = new Paint();
		backPaint.setAntiAlias(true);
		backPaint.setStrokeCap(Paint.Cap.ROUND);
		backPaint.setColor(Color.WHITE);

		lightPaint = new Paint();
		lightPaint.setAntiAlias(true);
		lightPaint.setStrokeCap(Paint.Cap.ROUND);
		lightPaint.setColor(Color.WHITE);
		lightPaint.setAlpha(168);

		linePaint = new Paint();
		linePaint.setAntiAlias(true);
		linePaint.setStrokeCap(Paint.Cap.ROUND);
		linePaint.setStrokeWidth(LINE_WIDTH);

		invalidate();
	}

}
